package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;
    /**
     * 商品分页查询
     * @param page
     * @param rows
     * @param id
     * @param cid
     * @param bid
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "id",required = false)Long id,
            @RequestParam(value = "categoryId",required = false)Long cid,
            @RequestParam(value = "brandId",required = false)Long bid,
            @RequestParam(value = "saleable",required = false)Boolean saleable){

        return ResponseEntity.ok(this.spuService.pageQuery(page,rows,id,cid,bid,saleable));
    }


    /*根据id批量查询sku*/
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuByIds(@RequestParam("ids") List<Long> ids){

        return ResponseEntity.ok(this.skuService.listSkuByIds(ids));

    }

    /**
     * 根据spuId查询对应的sku的集合
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySpu(
            @RequestParam("id")Long spuId){

        return ResponseEntity.ok(this.skuService.listSkuBySpu(spuId));
    }

    /*根据spuid查询商品详情*/
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> listSpuById(@RequestParam("id") Long id){

        return ResponseEntity.ok(this.spuDetailService.listSpuDetailById(id));
    }


/*根据spuId查询spu的所有规格参数值*/
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> queryParam (
            @RequestParam("id") Long id,
            @RequestParam(value = "searching",required = false) Boolean searching){

        return ResponseEntity.ok(this.spuService.queryParam(id,searching));

    }

    /**
     * 根据spuId查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.spuService.queryGoodsById(id));
    }

    /**
     * 根据spuId查询spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.spuService.querySpuById(id));
    }

    /**
     * 修改商品的上下架状态
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> modifySaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable")Boolean saleable){

        this.spuService.modifySaleable(id,saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }


}
